﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

namespace GhostGame
{
    public partial class _Default : DataObjects.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.user = this.GetCurrentUser();
            if (!this.IsPostBack)
            {
                this.FreshBind();
            }
        }

        private void FreshBind()
        {
            this.TBUserName.Text = this.user.Name;
            this.GameList.DataSource = DataObjects.GameContainer.Rooms.OrderByDescending(r => r.LastUpdatedTime);
            this.GameList.DataBind();
        }

        private DataObjects.User user;

        protected void SetName_Click(object sender, EventArgs e)
        {
            this.user.Name = this.TBUserName.Text;
        }

        protected void NewRoom_Click(object sender, EventArgs e)
        {
            if (DataObjects.GameContainer.Rooms.FirstOrDefault(r => r.Holder.ID == this.user.ID)
                == default(DataObjects.GhostRoom))
            {
                DataObjects.GhostRoom room = DataObjects.GhostRoom.CreateRoom();
                room.Name = this.TBRoomName.Text;
                room.Holder = this.user;
            }
            this.Response.Redirect("~/GhostRoom.aspx");
        }

        protected void GameList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "JoinRoom":
                    DataObjects.GhostRoom room = DataObjects.GameContainer.Rooms.FirstOrDefault(
                        r => r.Members.Select(u => u.ID).Contains(this.user.ID));
                    int newRoomID = int.Parse(e.CommandArgument as string);
                    if (room != default(DataObjects.GhostRoom) && newRoomID != room.ID)
                    {
                        room.RemoveMember(this.user.ID);
                        room.UpdateTime();
                    }
                    else
                    {
                        room = DataObjects.GameContainer.Rooms.FirstOrDefault(r => r.Holder.ID == this.user.ID);
                        if (room != default(DataObjects.GhostRoom) && newRoomID != room.ID)
                        {
                            room.ClearRoom();
                            DataObjects.GameContainer.Rooms.Remove(room);
                        }
                    }

                    DataObjects.GhostRoom newRoom = DataObjects.GameContainer.Rooms.First(r => r.ID == newRoomID);
                    if (newRoom == default(DataObjects.GhostRoom))
                    {
                        this.Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        newRoom.AddMember(this.user.ID);
                        this.Response.Redirect("~/GhostRoom.aspx");
                    }
                    break;
                default:
                    break;
            }
        }

        protected void GameList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ((Button)e.Item.Controls[1]).Text = ((DataObjects.GhostRoom)e.Item.DataItem).Name.ToString();
            ((Button)e.Item.Controls[1]).CommandArgument = ((DataObjects.GhostRoom)e.Item.DataItem).ID.ToString();
        }

        protected void Refresh_Click(object sender, EventArgs e)
        {
            this.FreshBind();
        }
    }
}